﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;
using LinFu.AOP.Interfaces;

namespace BrainTechLLC
{
	[Serializable, AttributeUsage(AttributeTargets.Method)]
	public class SingleThreadExecutionAttribute : GenericAttributeString, IReadableAttribute
	{
		public string LockID { get { return Val; } }

		public SingleThreadExecutionAttribute(string lockID) : base(lockID) { }

		public void DoWorkBefore(IInvocationContext context, object thisObject, Type t)
		{
			object lockable = Lookups.LockableLookup.Get(LockID);
			context.ExtraInfo = lockable;
			Monitor.Enter(lockable);
		}

		public void DoWorkAfter(IInvocationContext context, object thisObject)
		{
			Monitor.Exit(context.ExtraInfo);
		}
	}
}

